package cn.com.api.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiT_member;
import cn.com.api.entity.ApiT_member_entrepreneur;
import cn.com.api.entity.ApiT_member_partner;
import cn.com.api.entity.ApiT_member_partner_task;
import cn.com.api.mapper.ApiT_memberMapper;
import cn.com.api.mapper.ApiT_member_entrepreneurMapper;
import cn.com.api.mapper.ApiT_member_partnerMapper;
import cn.com.api.mapper.ApiT_member_partner_taskMapper;
import cn.com.api.mapper.ApiTmemberMapper;
import cn.com.api.mapper.ApiU_followMapper;
import cn.com.api.service.ApiMemberService;
import cn.com.api.service.ApiT_memberService;
import cn.com.api.service.ApiT_member_entrepreneurService;
import cn.com.api.service.impl.base.T_member_entrepreneurServiceImplBase;
import cn.com.util.DateUtil;
import cn.com.util.PageInfo;
import cn.com.util.Utils;

/**
 * 
 **/
@Service
public class ApiT_member_entrepreneurServiceImpl extends T_member_entrepreneurServiceImplBase
		implements ApiT_member_entrepreneurService {
	@Autowired
	private ApiT_member_entrepreneurMapper apiT_member_entrepreneurMapper;
	@Autowired
	private ApiT_member_partnerMapper apiT_member_partnerMapper;
	@Autowired
	private ApiT_member_partner_taskMapper apiT_member_partner_taskMapper;
	@Autowired
	private ApiT_memberMapper memberMapper;
	@Autowired
	private ApiTmemberMapper apiTmemberMapper;

	@Autowired
	private ApiU_followMapper apiU_followMapper;

	public List<ApiT_member_entrepreneur> findT_member_entrepreneursByMap(Map paramMap, PageInfo page) {
		return apiT_member_entrepreneurMapper.findT_member_entrepreneursByMapListPage(paramMap, page);
	}

	@Override
	public List<Map<String, Object>> findMember_entrepreneursByMap(Map map) {
		return apiT_member_entrepreneurMapper.findMember_entrepreneursByMap(map);
	}

	// 获取关注的企业主
	public List<ApiT_member_entrepreneur> findEntreListByFollowType(String user_id, Integer type, PageInfo page) {
		return apiT_member_entrepreneurMapper.findEntreListByFollowTypeListPage(user_id, type, page);
	}

	@Override
	public ApiT_member_entrepreneur findMemberEntById(String id) {
		// TODO Auto-generated method stub
		return apiT_member_entrepreneurMapper.findMemberEntById(id);
	}

	public ApiT_member_entrepreneur getEntrepreneurIdByUserId(Integer cid) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_id", cid);
		List<Map<String, Object>> list = apiT_member_entrepreneurMapper.findMember_entrepreneursByMap(map);
		Map<String, Object> result = list.get(0);
		String entrepreneurId = result.get("ID").toString();
		ApiT_member_entrepreneur apiT_member_entrepreneur = apiT_member_entrepreneurMapper
				.findT_member_entrepreneurById(Integer.valueOf(entrepreneurId));
		return apiT_member_entrepreneur;
	}

	public Map<String, Object> applyEntrepreneur(ApiT_member_entrepreneur entrepreneur) {
		Map<String, Object> map = new HashMap<String, Object>();
		Boolean validate = true;
		if (entrepreneur.getReferee_id() != null && !"".equals(entrepreneur.getReferee_id())) {
			validate = validateMember(entrepreneur.getUser_id() + "", entrepreneur.getReferee_id(),
					entrepreneur.getCorp_id(), map);
		}
		if (validate) {
			Date now = new Date();
			if (entrepreneur.getReferee_id() != null && !"".equals(entrepreneur.getReferee_id())) {
				map.clear();
				map.put("partner_code", entrepreneur.getReferee_id());
				ApiT_member_partner apiT_member_partner = apiT_member_partnerMapper.findTmemberpartnersByMap(map);
				ApiT_member_partner_task apiT_member_partner_task = apiT_member_partner_taskMapper
						.findT_member_partner_taskById(Integer.valueOf(apiT_member_partner.getTask_id()));
				entrepreneur.setReferee_type(0);// 推荐
				entrepreneur.setBonus_ratio(apiT_member_partner.getBonus_ratio());

				entrepreneur.setUpdate_time(DateUtil.dateToString(now, "yyyy-MM-dd HH:mm:ss"));
				Date dateAfter = DateUtil.getDateAfter(now, 1, apiT_member_partner_task.getSign_term());
				entrepreneur.setReferee_deadline(DateUtil.dateToString(dateAfter, "yyyy-MM-dd HH:mm:ss"));
			}
			entrepreneur.setStatus(0);
			entrepreneur.setApply_time(DateUtil.dateToString(now, "yyyy-MM-dd HH:mm:ss"));
			map.clear();
			map.put("user_id", entrepreneur.getUser_id());
			PageInfo page = new PageInfo();
			page.setCurrentPage(1);
			page.setShowCount(5);
			List<ApiT_member_entrepreneur> list = apiT_member_entrepreneurMapper
					.findT_member_entrepreneursByMapListPage(map, page);
			if (list != null && list.size() > 0) {
				entrepreneur.setId(list.get(0).getId());
				apiT_member_entrepreneurMapper.updateT_member_entrepreneur(entrepreneur);
			} else {
				apiT_member_entrepreneurMapper.insertT_member_entrepreneur(entrepreneur);
			}
			map.clear();
			map.put("cid", entrepreneur.getUser_id());
			map.put("verify_entrepreneur", "2");
			if (entrepreneur.getReferee_id() != null && !"".equals(entrepreneur.getReferee_id())) {
				map.put("referee_uid", entrepreneur.getReferee_id());
			}
			apiTmemberMapper.modifyApiTmemberByCuserid(map);
			map.clear();
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "操作成功");
			map.put(Utils.DATAS, entrepreneur);
		}
		return map;
	}

	/**
	 * 验证机构和推荐人
	 * 
	 */
	public Boolean validateMember(String user_id, String partner_code, String corp_code, Map<String, Object> map) {
		Boolean result = false;
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("partner_code", partner_code);
		List<ApiT_member> list = memberMapper.findT_membersByMap(conditions);
		if (list != null && list.size() > 0) {
			ApiT_member apiT_member = list.get(0);
			if (user_id.equals(apiT_member.getC_id())) {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "推荐人不能为自己");
			} else {
				if (apiT_member.getVerify_partner() == 1) {
					conditions.clear();
					conditions.put("user_id", apiT_member.getC_id());
					ApiT_member_partner apiT_member_partner = apiT_member_partnerMapper
							.findTmemberpartnersByMap(conditions);
					if (corp_code.equals(apiT_member_partner.getCorp_id())) {
						result = true;
						map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
						map.put(Utils.MESSAGE, "验证成功");
					} else {// g
						map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
						map.put(Utils.MESSAGE, "该核伙人不属于机构");
					}
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
					map.put(Utils.MESSAGE, "该人员不是核伙人");
				}
			}

		} else {
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "该人员不存在");
		}
		return result;
	}

	@Override
	public Map<String, Object> getMemberByUserId(String conn_id, String user_id) {

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> param = new HashMap<String, Object>();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("conn_id", conn_id);
		param.put("user_id", user_id);
		param.put("conn_id", conn_id);
		ApiT_member apiT_member = memberMapper.findT_memberById(Integer.parseInt(conn_id));
		ApiT_member_entrepreneur apiT_member_entrepreneur = apiT_member_entrepreneurMapper.findMemberEntByUserId(conn_id);
		
		if(apiT_member_entrepreneur == null){
			param.put("type", 0);
			int follow_status = apiU_followMapper.getFollowIsSub(param);
			if (apiT_member != null) {
				resultMap.put("user_name", apiT_member.getC_tname());
				resultMap.put("corp_brief", "");
				resultMap.put("c_nick", apiT_member.getC_nick());
				resultMap.put("c_avatar", apiT_member.getC_avatar());
			} else {
				resultMap.put("user_name", "");
				resultMap.put("corp_brief", "");
				resultMap.put("c_nick", "");
				resultMap.put("c_avatar", "");
			}
			resultMap.put("follow_status", follow_status);
			resultMap.put("conn_status", 0);
		}else{			
			map.put("id", apiT_member_entrepreneur.getId());
			map.put("verify_entrepreneur", 1);
			List<Map<String, Object>> list = apiT_member_entrepreneurMapper.findMember_entrepreneursByMap(map);
			//param.put("conn_id", apiT_member_entrepreneur.getId());
			if (list != null && list.size() >= 1) {
				param.put("type", 1);
				int follow_status = apiU_followMapper.getFollowIsSub(param);
				for (Map<String, Object> map2 : list) {
					
					resultMap.put("user_name", map2.get("USER_NAME"));
					resultMap.put("corp_brief", map2.get("CORP_BRIEF"));
					resultMap.put("c_nick", map2.get("C_NICK"));
					resultMap.put("c_avatar", map2.get("C_AVATAR"));
					
					resultMap.put("id", map2.get("ID"));
					resultMap.put("corp_fname", map2.get("CORP_FNAME"));
					resultMap.put("corp_sname", map2.get("CORP_SNAME"));
					resultMap.put("logo_img", map2.get("LOGO_IMG"));
				}
				resultMap.put("follow_status", follow_status);
				resultMap.put("conn_status", 1);
			} else {
				param.put("type", 0);
				int follow_status = apiU_followMapper.getFollowIsSub(param);
				if (apiT_member != null) {
					resultMap.put("user_name", apiT_member.getC_tname());
					resultMap.put("corp_brief", "");
					resultMap.put("c_nick", apiT_member.getC_nick());
					resultMap.put("c_avatar", apiT_member.getC_avatar());
				} else {
					resultMap.put("user_name", "");
					resultMap.put("corp_brief", "");
					resultMap.put("c_nick", "");
					resultMap.put("c_avatar", "");
				}
				resultMap.put("follow_status", follow_status);
				resultMap.put("conn_status", 0);
			}
		}
		
		return resultMap;
	}

	@Override
	public int updateT_member_entrepreneur_brief(ApiT_member_entrepreneur t_member_entrepreneur) {
		// TODO Auto-generated method stub
		return apiT_member_entrepreneurMapper.updateT_member_entrepreneur_brief(t_member_entrepreneur);
	}

	public List<ApiT_member_entrepreneur> getEntrepreneurList(String keyword, PageInfo page) {
		Map<String, Object> param = new HashMap<String, Object>();
		if (keyword != null && !"".equals(keyword)) {
			param.put("name", keyword);
		}
		return apiT_member_entrepreneurMapper.findEntrepreneursByMapListPage(param, page);
	}
}
